package com.cyt.work.question.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.BaseUtil;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.question.dao.QuestionUserDao;
import com.cyt.work.question.model.dto.QuestionUserDto;
import com.cyt.work.question.model.po.QuestionUser;
import com.cyt.work.question.model.vo.QuestionUserImportVo;
import com.cyt.work.question.service.IQuestionUserService;
import com.cyt.wx.model.po.WxEducationUser;
import com.cyt.wx.model.vo.WxEducationUserVo;
import com.cyt.wx.service.WxEducationUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionUserServiceImpl extends BaseServiceImpl<QuestionUser> implements IQuestionUserService {

    private static Logger logger = LoggerFactory.getLogger(QuestionUserServiceImpl.class);

    @Autowired
    private WxEducationUserService wxEducationUserService;
    @Autowired
    private QuestionUserDao questionUserDao;
    @Autowired
    public void setBaseDao(QuestionUserDao questionUserDao) {
        this.baseDao = questionUserDao;
    }

    @Override
    public List<QuestionUserDto> selectByQuestionId(String questionId, String status) {
        return questionUserDao.selectByQuestionId(questionId, status);
    }

    @Override
    public List<QuestionUserDto> selectQrcodeByQuestionId(String questionId) {
        List<QuestionUserDto> questionUserList = questionUserDao.selectQrcodeByQuestionId(questionId);
        if (questionUserList != null && questionUserList.size() > 0) {
            for (QuestionUserDto questionUserDto : questionUserList) {
                if (StringUtils.isNotEmpty(questionUserDto.getUserName())) {
                    questionUserDto.setUserName(BaseUtil.decodeBase64(questionUserDto.getUserName()));
                }
            }
        }
        return questionUserList;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ReturnResult saveQuestionUser(String questionId, String userJson) {
        // 删除以前的数据
        QuestionUser questionUserSearch = new QuestionUser();
        questionUserSearch.setQuestionId(questionId);
        questionUserDao.delete(questionUserSearch);
        List<QuestionUser> userList = JSONArray.parseArray(userJson, QuestionUser.class);
        if (userList != null && userList.size() > 0) {
            for (QuestionUser questionUser : userList) {
                questionUser.setId(BaseUtil.uId());
                questionUser.setQuestionId(questionId);
                questionUser.setCreateTime(DateUtil.date());
            }
            questionUserDao.insertList(userList);
        }
        return new ReturnResult();
    }

    @Override
    public ReturnResult saveQuestionAllWxEducationUser(String applyId, String questionId, SysUser sysUser) {
        WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
        wxEducationUserVo.setStatus(BaseConstant.BASE_STATUS_YES);
        wxEducationUserVo.setCreateUser(sysUser.getId());
        List<WxEducationUser> wxEducationUserList = wxEducationUserService.listExample(wxEducationUserVo);
        this.insertByWxEducationUser(applyId, questionId, wxEducationUserList);
        return new ReturnResult();
    }

    @Override
    public ReturnResult saveQuestionWxEducationUserByStatus(String applyId, String questionId, String objectType, SysUser sysUser) {
        // 需要和创建人联系上
        WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
        wxEducationUserVo.setStatus(BaseConstant.BASE_STATUS_YES);
        wxEducationUserVo.setUserType(objectType);
        wxEducationUserVo.setCreateUser(sysUser.getId());
        List<WxEducationUser> wxEducationUserList = wxEducationUserService.listExample(wxEducationUserVo);
        this.insertByWxEducationUser(applyId, questionId, wxEducationUserList);
        return new ReturnResult();
    }

    @Override
    public void updateStatus(String questionId, String userId) {
        questionUserDao.updateStatus(questionId, userId);
    }

    @Override
    public void insertList(List<QuestionUser> list) {
        questionUserDao.insertList(list);
    }

    @Override
    public List<QuestionUserImportVo> selectExportUserByQuestionId(String questionId) {
        return questionUserDao.selectExportUserByQuestionId(questionId);
    }

    @Override
    public ReturnResult updateQuestionUser(String questionId, String userJson) {
        // 删除未提交的数据，不能删除已提交的
        QuestionUser questionUserSearch = new QuestionUser();
        questionUserSearch.setQuestionId(questionId);
        questionUserSearch.setStatus(BaseConstant.QUESTION_USER_STATUS_NO_SUBMIT);
        questionUserDao.delete(questionUserSearch);
        List<QuestionUser> userList = JSONArray.parseArray(userJson, QuestionUser.class);
        if (userList != null && userList.size() > 0) {
            for (QuestionUser questionUser : userList) {
                questionUser.setId(BaseUtil.uId());
                questionUser.setQuestionId(questionId);
                questionUser.setCreateTime(DateUtil.date());
            }
            questionUserDao.insertList(userList);
        }
        return new ReturnResult();
    }

    @Override
    public void updateStatusByUnbindUser(String userId) {
        QuestionUser questionUser = new QuestionUser();
        questionUser.setStatus(BaseConstant.QUESTION_USER_STATUS_NO_SUBMIT);
        //筛选条件
        Example example = new Example(QuestionUser.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(userId)) {
            criteria.andEqualTo("userId", userId);
        }
        questionUserDao.updateByExampleSelective(questionUser, example);
    }

    private void insertByWxEducationUser(String applyId, String questionId, List<WxEducationUser> wxEducationUserList) {
        QuestionUser questionUserSearch = new QuestionUser();
        questionUserSearch.setQuestionId(questionId);
        questionUserSearch.setStatus(BaseConstant.QUESTION_USER_STATUS_NO_SUBMIT);
        questionUserDao.delete(questionUserSearch);
        List<QuestionUser> userList = new ArrayList<>();
        if (wxEducationUserList != null && wxEducationUserList.size() > 0) {
            for (WxEducationUser wxEducationUser : wxEducationUserList) {
                QuestionUser questionUser = new QuestionUser();
                questionUser.setId(BaseUtil.uId());
                questionUser.setApplyId(applyId);
                questionUser.setUserId(wxEducationUser.getId());
                questionUser.setStatus(BaseConstant.QUESTION_USER_STATUS_NO_SUBMIT);
                questionUser.setQuestionId(questionId);
                questionUser.setCreateTime(DateUtil.date());
                userList.add(questionUser);
            }
        }
        if (userList != null && userList.size() > 0) {
            questionUserDao.insertList(userList);
        }
    }
}
